package com.entidades;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.ExecutionException;

import android.content.Context;
import android.content.res.Resources;

import com.CustomClasses.FetchXML;
import com.bolao.MainTabActivity;
import com.bolao.R;

public class Team{

	private static final String ASSOCIATIONS_ASSOCIATION = "/associations/association=";
	private static final String NEWS_RSS_XML = "/news/rss.xml";
	private int id;
	List<Player> players;
	private String name;

	private static List<Team> teams;
	private String flag;
	private List <News> news;
	private String rss;

	private int points;
	private int wins;
	private int losses;
	private int drawn;
	private int goalScore;
	private int gamesPlayed;
	private boolean isScoreSet = false;
	
	private static final String tagName = "team";
	private static final  String[] fieldNames = new String[]{"id", "name", "flag", "fifarss"};
	
	
	private static final int LIST_ID = 0;
	
	private static final int LIST_NAME = 1;
	
	private static final int LIST_FLAG = 2;
	
	private static final int LIST_FIFA = 3;
	
	public static final String xmlFileName = "TeamList.xml";
	
	private static final String URL = "http://www.worldcupcompendium.com/entities/TeamList.xml";
	
	public String getFlag() {
		return flag;
	}

	public int getPoints() {
		getScore();
		return points;
	}

	public int getWins() {
		getScore();
		return wins;
	}

	public int getLosses() {
		getScore();
		return losses;
	}

	public int getDrawn() {
		getScore();
		return drawn;
	}

	public int getGoalScore() {
		getScore();
		return goalScore;
	}
	
	private void getScore(){
		
		if(!isScoreSet){
		List<Match> list = Match.getMatchesOfTeam(this);
		points = 0;
		goalScore = 0;
		wins = 0;
		drawn = 0;
		losses = 0;
		gamesPlayed = 0;
		for(Match m: list){
			switch (m.getResult()){
			case DRAWN:
				points++;
				drawn++;
				gamesPlayed++;
				break;
			case WIN_TEAM_A:
				goalScore += m.getScoreTA()-m.getScoreTB();
				gamesPlayed++;
				if(m.getTeamA().equals(this)){
				wins++;
				points +=3;
				
				} else if(m.getTeamB().equals(this)){
					losses++;
				}
				break;
			case WIN_TEAM_B:
				goalScore += m.getScoreTB()-m.getScoreTA();
				gamesPlayed++;
				if(m.getTeamB().equals(this)){
					wins++;
					points +=3;
				} else if(m.getTeamA().equals(this)) {
					losses++;
				}
				break;
			case NOT_PLAYED:
				break;		
			}			
		}
		isScoreSet = true;
		}
	}
	
	
	
	public String getLocalizedName() {
		Context context = MainTabActivity.getmContext();
		Resources resources = context.getResources();
		return resources.getString(resources.getIdentifier(getName(), "string",  context.getPackageName()));
	}

	private String getName() {
		return name;
	}
	public Team(){
		
		players = new ArrayList<Player>(23);
	}
	private Team(Integer id, String name, String flag, String rss) {
		this.id = id;
		this.name = name;
		this.flag = flag;
		this.rss = rss;
		
		
	}
	public int getId() {
		return id;
	}
	
	public static Team getById(int id){
		Team r = null;
		for(Team t:getTeams()){
			if(t.getId()==id){
				r =t;
				break;
			}
		}
		return r;
	}

	public static List<Team> getTeams() {
		if(teams==null){
			try {
				List<List<String>> list = new FetchXML().execute(URL, tagName, fieldNames).get();
				teams = new ArrayList<Team>();
				for(List<String> l: list){
					teams.add(new Team(Integer.valueOf(l.get(LIST_ID)), l.get(LIST_NAME), l.get(LIST_FLAG), l.get(LIST_FIFA)));
					}	
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (ExecutionException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
		
		class NameComparator implements Comparator<Team> {
		    public int compare(Team team, Team otherTeam) {
		        return team.getLocalizedName().
		                compareTo(otherTeam.getLocalizedName());
		    }
		}
		
		NameComparator comparator =new NameComparator();
		Collections.sort(teams, comparator);
		return teams;
		
	}
	
	public List<News> getNews(){
		if(news==null){
		news = new ArrayList<News>();
		Context context = MainTabActivity.getmContext();
		Resources resources = context.getResources();
		String url = resources.getString(R.string.fifaRSS);
		String posUrl =  ASSOCIATIONS_ASSOCIATION + rss + NEWS_RSS_XML;
		news = News.getNews(url, posUrl);
		}
		return news;
	}

	public int getGamesPlayed() {
		
		return gamesPlayed;
	}

	

}
